package com.venus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.venus.entity.Module;
import com.venus.entity.Role;
import com.venus.entity.RoleModule;
import com.venus.entity.User;
import com.venus.service.ModuleService;
import com.venus.service.RoleModuleService;
import com.venus.service.RoleService;
import com.venus.service.UserService;
import com.venus.util.PageUtil;
import com.venus.util.Result;
import com.venus.util.TreeNode;
import com.venus.util.TreeNodeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * role角色表 前端控制器
 * </p>
 *
 * @author Venus
 * @since 2020-06-08
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private ModuleService moduleService;

    @Resource
    private RoleModuleService roleModuleService;


    @RequestMapping("/select")
    public Object selectRole(Role role, PageUtil pu) {
        //获取分页信息
        //getPage()为当前页码  pu.getLimit()为每页显示条数
        Page<Role> page = new Page<Role>(pu.getPage(), pu.getLimit());
        //创建实体包装类   用于存放查询条件
        QueryWrapper<Role> select = new QueryWrapper<Role>();
        //根据名字模糊查询
        if (role.getRoleName() != null && !role.getRoleName().isEmpty()) {
            select.like("role_name", role.getRoleName());
        }
        //分页查询方法
        page = roleService.page(page, select);

        return new Result(0, "success", page.getRecords(), page.getTotal());
    }


    @RequestMapping("/create")
    public Object createRole(Role role) {
        Role roleUp = roleService.getOne(new QueryWrapper<Role>().eq("role_name", role.getRoleName()));
        if (roleUp == null) {
            boolean create = roleService.save(role);
            if (create == true) {
                return new Result(0, "添加成功");
            } else {
                return new Result(1, "添加失败！");
            }
        } else {
            return new Result(2, "名字重复！换一个名字吧！");
        }

    }


    @RequestMapping("/update")
    public Object updateRole(Role role) {


        Role roleUp = roleService.getOne(new QueryWrapper<Role>().eq("role_name", role.getRoleName()));
        if (roleUp == null) {
            boolean update = roleService.updateById(role);
            if (update == true) {
                return new Result(0, "修改陈功");
            } else {
                return new Result(1, "修改失败！");
            }
        } else {
            return new Result(2, "名字重复！换一个名字吧！");
        }

    }


    @RequestMapping("/delete")
    public Object deleteRole(Integer rid) {
        boolean del = roleService.removeById(rid);
        if (del == true) {
            return new Result(0, "删除陈功");
        } else {
            return new Result(1, "删除失败！");
        }
    }


    /**
     * 给角色保存权限
     *
     *
     * @param ids 权限id集合  rid角色id
     * @return 成功或者失败
     */
    @RequestMapping("/setRoleModule")
    public Object setSysRights(@RequestParam("ids") List<Integer> ids, Integer rid) {
        System.out.println(ids);
        /* 保存角色的权限，是保存在中间表
         * 一个角色很多权限，所以是批量添加
         * 设置权限时，会包含原有权限，原有权限数据库中已保存
         * 继续保存会重复，所以保存前先去数据库删除原数据
         * */
        //1.根据普通列rid，条件删
        QueryWrapper<RoleModule> qw=new QueryWrapper<RoleModule>();
        qw.eq("role_id", rid);
        roleModuleService.remove(qw);
        //2.保存
        List<RoleModule> list=new ArrayList<RoleModule>();
        for (Integer mid : ids) {
            list.add(new RoleModule(rid,mid));
        }
        roleModuleService.saveBatch(list);
        return new Result(0,"保存成功！");
    }

    /**
     * 根据角色id，获取一个完整的树，树上勾选了这个角色的权限
     *
     * @param rid 角色id
     * @return 被勾选了部分节点的树形菜单List<TreeNode>
     */
    @RequestMapping("/getModByRidTrue")
    public Object getModByRidTrue(Integer rid) {
        //1.首选需要一个完整的树
        //1-1，需要完整的树，先得有完整的模块，然后调用TreeNodeUtil里的getTree转化
        List<Module> allModuleList = moduleService.list();
        //1-2，转化成树
        List<TreeNode> allTreeList = TreeNodeList.getTree(allModuleList);
        //2.获取这个角色拥有的模块
        List<Module> moduleList = moduleService.getModByRid(rid);
        //3.如果角色模块的某个id和树的某个节点的id一致，就把这个节点check=true
        return TreeNodeList.getCheckedTree(moduleList, allTreeList);
    }



}
